<?php
namespace app\controllers;


use app\logic\TagLogic;
use yii;

class TagController extends BackendController
{

    /**
     * 标签列表
     */
    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $tagLogic = new TagLogic();
        $this->data['breadcrumbs'] = '标签管理';
        return $this->render('index', [
            'tag' => $tagLogic->getTag($query, $page),
            'query' => $query
        ]);
    }
    /**
     * 添加标签
     */
    public function actionAdd()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['标签管理' => U('tag/index'), ' 添加标签' => ''];
            return $this->render('add', [
                'actionUrl' => U('tag/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $tagLogic = new TagLogic();
            $logicRes = $tagLogic->addTag($params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增标签成功');
                jump('tag/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增标签失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }
    /**
     * 编辑标签
     */
    public function actionEdit()
    {
        $tagLogic = new TagLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('tag_id');
            $tag = $tagLogic->getById($id);
            $this->data['breadcrumbs'] = ['标签管理' => U('tag/index'), $tag['tag_name'] => '', '更新' => ''];
            return $this->render('edit', [
                'tag' => $tag,
                'actionUrl' => U('tag/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('tag_id');
            $logicRes = $tagLogic->editTagById($id, $params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新标签成功');
                jump('tag/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新标签失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }

    /**
     * 删除标签
     */
    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('tag_id');
        $tagLogic = new TagLogic();
        $logicRes = $tagLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改标签状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改标签状态失败');
        }
        return $this->redirect(U('tag/index'));
    }

    /**
     * 删除标签
     */
    public function actionDelete()
    {
        $id = Yii::$app->request->get('tag_id');
        $tagLogic = new TagLogic();
        $logicRes = $tagLogic->deleteById($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除标签成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除标签失败');
        }
        return $this->redirect(U('tag/index'));
    }



}